package task3;

import java.util.Random;
import java.util.Scanner;

public class LoginSystem extends userdao{
    static int op=0;
    public static void main(String[] args) {
    menu();

    }

    public static void menu() {
        Scanner sc=new Scanner(System.in);
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.忘记密码");
        System.out.println("4.退出");
        op=sc.nextInt();
        switch (op){
            case 1:{
            dengLu();
            break;
            }
            case 2:{
            zhuCe();
            break;
            }
            case 3:{
            forgetPassword();
            break;
            }
            case 4:break;
        }
    }

    public static String creatRandomNumber() {
        String rn="";
        int n=0;
        char[] c = {'1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','f','i','j','k'
        ,'l','n','m','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I'
        ,'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','X'};
        Random r = new Random();
        for(int i=0;i<5;i++){
            n=r.nextInt(62);
            rn+=c[n];
        }
        return rn;
    }

    public static void dengLu() {
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("请输入用户名：");
            String userName=sc.next();
            System.out.println("请输入密码：");
            String password = sc.next();
            if(select2(userName,password)) {
                System.out.println("你好，登录成功！");
                break;
            }else{
                System.out.println("用户名或密码错误！");
                System.out.println("请重新输入");
            }
        }
    }

    public static void zhuCe() {
        Scanner sc = new Scanner(System.in);
        String userName ="";
        while(true){
            System.out.println("请输入用户名：");
            userName = sc.next();
            if(select1(userName)){
                System.out.println("已存在此用户名，请更换");
            }else{
                break;
            }
        }

        String password1="";
        while(true){
            System.out.println("请输入密码：");
            password1 = sc.next();
            System.out.println("请再次输入密码：");
            String password2 = sc.next();
            if(password2.equals(password1)){
                System.out.println("注册成功");
                break;
            }else{
                System.out.println("两次密码输入不一致，请重新输入");
            }
        }
        insert(userName,password1);
    }

    public static void forgetPassword() {
        Scanner sc=new Scanner(System.in);
        String userName ="";
        while(true){
            System.out.println("请输入用户名：");
            userName = sc.next();
            if(select1(userName)){
                break;
            }else{
                System.out.println("此用户名不存在，请重新输入");
            }
        }
        while(true){
            String yanzhengMa=creatRandomNumber();
            System.out.println("请输入验证码："+yanzhengMa);
            String y = sc.next();
            if(y.equals(yanzhengMa)){
                while(true){
                    System.out.println("请输入新的密码：");
                    String password1 = sc.next();
                    System.out.println("请再次输入密码：");
                    String password2 = sc.next();
                    if(password2.equals(password1)){
                        changePassword(userName,password1);
                        System.out.println("修改密码成功");
                        break;
                    }else{
                        System.out.println("两次密码输入不一致，请重新输入");
                    }
                }
                break;
            }else{
                System.out.println("请输入正确的验证码");
            }
        }
    }

}
